/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.mio.codex.controllers;

import com.asseco.mio.codex.FirstLoginModel;
import com.asseco.persistence.service.HibernateUtil;
import java.util.List;
import org.appfuse.model.User;
import org.appfuse.service.GenericManager;
import org.appfuse.service.UserManager;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 *
 * @author bojan.dimovski
 */
public class FirstLoginCheck {

    private UserManager userManager = null;
    @Autowired
    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }







    protected GenericManager<FirstLoginModel, String> firstLoginManager;

    @Autowired
    public void setFirstLoginManager(@Qualifier("firstLoginManager") GenericManager<FirstLoginModel, String> firstLoginManager) {
        this.firstLoginManager = firstLoginManager;
    }


   

    public Boolean isFirstLogin(String username) {
        Boolean test = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(FirstLoginModel.class).add(Restrictions.eq("username",username));

        List result = criteria.list();
        session.getTransaction().commit();

        if (!result.isEmpty()) {
        FirstLoginModel x = (FirstLoginModel) result.get(0);
            test = x.isPasswordStatus();
        }
        
        return test;
    
    }

    public void addFirstUser(String username, String password) {
        FirstLoginModel user = new FirstLoginModel();
        user.setUsername(username);
        user.setPasswordStatus(true);
        user.setFirstPassword(password);
        firstLoginManager.save(user);
    }

    public String firstPassword(String username) {
        String password = null;

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(FirstLoginModel.class).add(Restrictions.eq("username",username));

        List result = criteria.list();
        session.getTransaction().commit();

        if(!result.isEmpty()) {
            FirstLoginModel y = (FirstLoginModel) result.get(0);

            password = y.getFirstPassword();
        }

        return password;
        
    }

  

}
